package services

import (
	"GOPAAS/internal/app/model"
	"GOPAAS/internal/utils"
	"github.com/gin-gonic/gin"
)

type ActivityList struct {
	Id             int64  `json:"id"`
	ResModelId     int64  `json:"res_model_id"`
	ResModel       string `json:"res_model"`
	ModelName      string `json:"model_name"`
	ResId          int64  `json:"res_id"`
	ResName        string `json:"res_name"`
	ActivityTypeId int64  `json:"activity_type_id"`
	ActivityType   string `json:"activity_type"`
	DateDeadline   string `json:"date_deadline"`
	Summary        string `json:"summary"`
	Note           string `json:"note"`
	CreateDate     string `json:"create_date"`
}

// GetUserActivityList 用户最近活动
func GetUserActivityList(c *gin.Context, user_id int64, page int, limit int) (res []ActivityList, err error) {
	db = getDB()
	lang, _ := c.Get("lang")
	var active []model.MailActivity
	offset := (page - 1) * limit
	db.Model(&model.MailActivity{}).Preload("Model").Preload("ActivityType").Where("user_id=?", user_id).Order("date_deadline desc,id desc").Offset(offset).Limit(limit).Find(&active)
	for _, v := range active {
		res = append(res, ActivityList{
			Id:             v.Id,
			ResModelId:     v.ResModelId,
			ResModel:       v.ResModel,
			ModelName:      utils.GetJsonName(v.Model.Name, lang.(string)),
			ResId:          v.ResId,
			ResName:        v.ResName,
			ActivityTypeId: v.ActivityTypeId,
			ActivityType:   utils.GetJsonName(v.ActivityType.Name, lang.(string)),
			DateDeadline:   v.DateDeadline.Format("2006-01-02"),
			CreateDate:     v.CreateDate.Format("2006-01-02 15:04:05"),
			Summary:        v.Summary,
			Note:           v.Note,
		})
	}
	return
}
